package com.example.stock.tencent;

import cn.hutool.json.JSONUtil;

import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

public class DingDingBot {

    public static void main(String[] args) {
        DingDingBot.send("测试消息");
    }

    public static void send(String msg) {
        // 替换成你的钉钉机器人WebHook URL
        String webhookUrl = "https://oapi.dingtalk.com/robot/send?access_token=992e3b98a18383cea975cabd0e2c4c63f83de91dc958d4f8ec48421c400daca0";

        Map<String,Object> map = new HashMap<String,Object>();
        Map<String,Object> contentMap = new HashMap<String,Object>();
        contentMap.put("content","发财："+msg);
        map.put("msgtype","text");
        map.put("text",contentMap);

        // 构建要发送的JSON数据
//        String jsonPayload = "{\"msgtype\": \"text\", \"text\": {\"content\": \" 发财：Hello, this is a message from Java!\"}}";
        String jsonPayload = JSONUtil.toJsonStr(map);
        try {
            // 创建URL对象
            URL url = new URL(webhookUrl);
            // 打开连接
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            // 设置请求方法为POST
            connection.setRequestMethod("POST");
            // 允许发送数据
            connection.setDoOutput(true);
            // 设置请求头
            connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");

            // 发送数据
            try (OutputStream os = connection.getOutputStream()) {
                byte[] input = jsonPayload.getBytes("utf-8");
                os.write(input, 0, input.length);
            }

            // 获取响应码
            int responseCode = connection.getResponseCode();
            System.out.println("Response Code: " + responseCode);

            // 检查响应码
            if (responseCode == 200) {
                System.out.println("Message sent successfully.");
            } else {
                System.out.println("Failed to send message.");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void sendMarkDown(String title,String msg) {
        // 替换成你的钉钉机器人WebHook URL
        String webhookUrl = "https://oapi.dingtalk.com/robot/send?access_token=992e3b98a18383cea975cabd0e2c4c63f83de91dc958d4f8ec48421c400daca0";

        Map<String,Object> map = new HashMap<String,Object>();
        Map<String,Object> contentMap = new HashMap<String,Object>();
        contentMap.put("text",msg);
        contentMap.put("title","发财："+title);
        map.put("msgtype","markdown");
        map.put("markdown",contentMap);

        // 构建要发送的JSON数据
//        String jsonPayload = "{\"msgtype\": \"text\", \"text\": {\"content\": \" 发财：Hello, this is a message from Java!\"}}";
        String jsonPayload = JSONUtil.toJsonStr(map);
        try {
            // 创建URL对象
            URL url = new URL(webhookUrl);
            // 打开连接
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            // 设置请求方法为POST
            connection.setRequestMethod("POST");
            // 允许发送数据
            connection.setDoOutput(true);
            // 设置请求头
            connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");

            // 发送数据
            try (OutputStream os = connection.getOutputStream()) {
                byte[] input = jsonPayload.getBytes("utf-8");
                os.write(input, 0, input.length);
            }

            // 获取响应码
            int responseCode = connection.getResponseCode();
            System.out.println("Response Code: " + responseCode);

            // 检查响应码
            if (responseCode == 200) {
                System.out.println("Message sent successfully.");
            } else {
                System.out.println("Failed to send message.");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

